#AbEngie Graphis Module
#By Red_Cell
#Open source with Apache 2.0 License


import PIL #导入万恶的PIL
from PIL import Image #导入PIL里面万恶的Image类
import os #导入万恶的操作系统库


Local=os.getcwd()+'\\Graphis\\'


#Add Transparency 添加透明度
def AT(img,Level=0):
  img = img.convert('RGBA')
  r, g, b, alpha = img.split()
  alpha = alpha.point(lambda i: i>0 and Level)
  img.putalpha(alpha)
  return img


#Mix 3 layers to a single photo 将三个层叠加
def Mix(L1,L2,Light=0):


  #Convert LightData to Transparency 将光照值转换成透明度
  FLight=Light*51

  #Read data from image. 读取图像数据
  L1data = Image.open(Local+L1)
  L2data = Image.open(Local+L2)

  #Get GeryGraph(GG) 得到亮度蒙版(简称GG)
  DarkOrigin=Image.open(Local+'Dark.png')
  GG=AT(DarkOrigin,FLight)


  #Get GreyGraph(GG) AlphaChannel 得到GG的Alpha通道
  if GG.mode in ('RGBA', 'LA') or (GG.mode == 'P' and 'transparency' in GG.info):
    GGalpha = GG.convert('RGBA').split()[-1]
    GG.putalpha(GGalpha)
    print(GGalpha)

  #Get L2 Alpha Channel 获取第二层的Alpha通道
  if L2data.mode in ('RGBA', 'LA') or (L2data.mode == 'P' and 'transparency' in L2data.info):
    l2alpha = L2data.convert('RGBA').split()[-1]
    L2data.putalpha(l2alpha)

  #Paste L2 to L1 L1和L2黏♂贴
  L1data.paste(L2data,mask=l2alpha)

  #Add grey 增加蒙版
  L1data.paste(GG,GGalpha)

  #Return 返回图像数据
  return L1data


#Test Mix 测试一遍
Image=Mix('BackGround.png','front.png',0)
Image.show()